'Copyright (c) 2008, Jonathan Allen

Imports System
Imports System.Collections.Generic
Imports System.Runtime.InteropServices
Imports System.Security.Cryptography
Imports System.Text

''' <summary>
''' The Request Token that is used to gain permission to a user's Netflix account.
''' </summary>
''' <remarks>
''' <list>
''' <item>Step 1: Generate a Request Token</item>
''' <item>Step 2: Have </item>
''' </list>
''' </remarks>
Public Class RequestToken
	Private m_Token As String
    Private m_TokenSecret As String
	Private m_PermissionUrl As String
	Private m_Converted As Boolean
	Private m_Connection As NetflixConnection
	Private m_RawData As String

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' 
    ''' </returns>
    ''' <remarks>
    ''' 
    ''' </remarks>
    Friend ReadOnly Property RawData() As String
        Get
            Return m_RawData
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="rawRequestToken">
    ''' 
    ''' </param>
    ''' <param name="connection">
    ''' 
    ''' </param>
    ''' <remarks>
    ''' 
    ''' </remarks>
    Friend Sub New(ByVal rawRequestToken As String, ByVal connection As NetflixConnection)
        Dim parsedData As New ClrExtensions.Net.Rest.QueryParameterCollection(rawRequestToken)
        m_Connection = connection
        m_RawData = rawRequestToken
        m_Token = parsedData("oauth_token")
        m_TokenSecret = parsedData("oauth_token_secret")
        m_PermissionUrl = NetflixUrls.PermissionLogin(connection, Me).ToUrlString
    End Sub

	''' <summary>
    ''' This is the URL to which developers need to send users, in order to gain the OAuth tokens necessary for the application to access the user's account.
	''' </summary>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' 
    ''' </returns>
    ''' <remarks>
    ''' 
    ''' </remarks>
	Public ReadOnly Property PermissionUrl() As String
		Get
			Return m_PermissionUrl
		End Get
	End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' </returns>
    ''' 
    ''' <remarks>
    ''' 
    ''' </remarks>
    Friend ReadOnly Property TokenSecret() As String
        Get
            Return m_TokenSecret
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value>
    ''' 
    ''' </value>
    ''' <returns>
    ''' 
    ''' </returns>
    ''' <remarks>
    ''' 
    ''' </remarks>
    Friend ReadOnly Property Token() As String
        Get
            Return m_Token
        End Get
    End Property

	''' <summary>
	''' This method converts a Request Token into an Access Token
	''' </summary>
    ''' <returns>
    ''' An AccessToken object corresponding to the current RequestToken.
    ''' </returns>
    ''' <remarks>
    ''' Do not call this method until after the user approves your application. Calling it prematurely will foul the Request Token.
    ''' </remarks>
    Public Function ConvertToAccessToken() As AccessToken
        If m_Converted Then Throw New InvalidOperationException("This method can only be called once.")
        Dim accessToken As WrapNetflix.AccessToken = m_Connection.ConvertRequestToAccessToken(Me)
        m_Converted = True
        Return accessToken
    End Function

End Class
